package util;

/**
 * Created by Tecnocom on 1/15/2015.
 */
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;




import com.gonfre.quientelo.MyActivity;

public class MyDataBase extends SQLiteOpenHelper {
    private String TABLE;
    private String SQL_CREATE;
    private MyActivity M;
    private DataBaseManager DBM;

    public MyDataBase(MyActivity contexto, DataBaseManager dbm, String table, String sql_create) {
        super(contexto, contexto.getDBTable(table), null, contexto.getVersionDB(table));
        this.TABLE = table;
        this.SQL_CREATE = sql_create;
        this.M = contexto;
        this.DBM = dbm;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("Trace MyDataBase 'onCreate'", "Se crea la tabla por primera vez. " + SQL_CREATE);
        //Se ejecuta la sentencia SQL de creación de la tabla
        db.execSQL(SQL_CREATE);
        DBM.initData(this.TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
        //NOTA: Por simplicidad del ejemplo aquí utilizamos directamente la opción de
        //      eliminar la tabla anterior y crearla de nuevo vacía con el nuevo formato.
        //      Sin embargo lo normal será que haya que migrar datos de la tabla antigua
        //      a la nueva, por lo que este método debería ser más elaborado.
        Log.i("Trace MyDataBase 'onUpgrade'", "No haga nada.");
        /*
        Log.i("Trace MyDataBase 'onUpgrade'", "Se borra y crea nuevamenta la tabla. " + SQL_CREATE);

        Log.i("Trace MyDataBase 'onUpgrade'", "versionAnterior:" + versionAnterior + " - versionNueva:" + versionNueva);

        //Se elimina la versión anterior de la tabla
        db.execSQL("DROP TABLE IF EXISTS " + this.TABLE);

        //Se crea la nueva versión de la tabla
        db.execSQL(SQL_CREATE);

        DBM.syncLocation();
        */
    }

    // Read records related to the search term
    public MyDBObject[] readItem(String query_item, String searchTerm, String column_item, String column_id) {

        // select query
        String sql = "";
        sql = query_item.replace("#p1",searchTerm);

        SQLiteDatabase db = this.getWritableDatabase();

        // execute the query
        Cursor cursor = db.rawQuery(sql, null);

        int recCount = cursor.getCount();

        MyDBObject[] ObjectItemData = new MyDBObject[recCount];
        int x = 0;

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                String objectName = cursor.getString(cursor.getColumnIndex(column_item));
                String objectId = cursor.getString(cursor.getColumnIndex(column_id));
                MyDBObject myObject = new MyDBObject(objectId, objectName);

                ObjectItemData[x] = myObject;

                x++;

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return ObjectItemData;
    }

    // Read records related to the search term
    public MyDBObject[] readItemCategory(String query_item, String searchTerm, String column_item, String column_id, String column_icon) {

        // select query
        String sql = "";
        sql = query_item.replace("#p1",searchTerm);

        SQLiteDatabase db = this.getWritableDatabase();

        // execute the query
        Cursor cursor = db.rawQuery(sql, null);

        int recCount = cursor.getCount();

        MyDBObject[] ObjectItemData = new MyDBObject[recCount];
        int x = 0;

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                String objectName = cursor.getString(cursor.getColumnIndex(column_item));
                String objectId = cursor.getString(cursor.getColumnIndex(column_id));
                String objectIcon= cursor.getString(cursor.getColumnIndex(column_icon));
                MyDBObject myObject = new MyDBObject(objectId, objectName, objectIcon);

                ObjectItemData[x] = myObject;

                x++;

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return ObjectItemData;
    }

    public String[] readStrItem(String query_item, String searchTerm, String column_item, String column_id) {

        // select query
        String sql = "";
        sql = query_item.replace("#p1",searchTerm);

        SQLiteDatabase db = this.getWritableDatabase();

        // execute the query
        Cursor cursor = db.rawQuery(sql, null);

        int recCount = cursor.getCount();

        String[] ObjectItemData = new String[recCount];
        int x = 0;

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                String objectName = cursor.getString(cursor.getColumnIndex(column_item));
                String objectId = cursor.getString(cursor.getColumnIndex(column_id));
                String myObject = objectName;

                ObjectItemData[x] = myObject;

                x++;

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return ObjectItemData;
    }

    public Cursor getAllItems(String query_item, String column_item, String column_id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query_item, null);
        return cursor;
    }
}
